package com.wswy.wyjk.ui.main;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.my.httpapi.api.annotation.ViewL;
import com.my.httpapi.api.baseView.BaseActivity;
import com.tencent.mmkv.MMKV;
import com.wswy.wyjk.R;
import com.wswy.wyjk.api.Api;
import com.wswy.wyjk.api.MMKVConstant;
import com.wswy.wyjk.api.entity.RequestParameter;
import com.wswy.wyjk.model.PracticeType;
import com.wswy.wyjk.ui.login.LoginActivity;
import com.wswy.wyjk.ui.login.model.LoginResponse;
import com.wswy.wyjk.ui.main.adapter.RankAdapter;
import com.wswy.wyjk.ui.main.event.CarType;
import com.wswy.wyjk.ui.main.event.InfoEvent;
import com.wswy.wyjk.ui.main.event.RankEvent;
import com.wswy.wyjk.ui.main.event.RankFinishEvent;
import com.wswy.wyjk.ui.main.model.RankResponse;
import com.wswy.wyjk.ui.practiceNew.view.RankItemDecoration;
import com.wswy.wyjk.ui.uitls.PracticeTypeUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

@ViewL(R.layout.layout_rank)
public class RankActivity extends BaseActivity {

    private RecyclerView rvRank;
    private RankAdapter rankAdapter;
    private String token;
    private MMKV mmkv;

    @Override
    protected void initData(Bundle savedInstanceState) {
        initView();
        initData();
    }

    @Override
    protected String showTitle() {
        return "排行榜";
    }

    @Override
    protected boolean statusBarIsBlack() {
        return true;
    }

    @Override
    protected void destroy() {
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        isShowTitleLeft(View.VISIBLE);
        rvRank = findViewById(R.id.rv_rank);
    }

    private void initData() {
        EventBus.getDefault().register(this);
        mmkv = MMKV.defaultMMKV();
        token = mmkv.decodeString(MMKVConstant.TOKEN);
        View emptyView = LayoutInflater.from(this).inflate(R.layout.layout_white_empty, null);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rankAdapter = new RankAdapter();
        rvRank.setLayoutManager(mLinearLayoutManager);
       // rvRank.addItemDecoration(new RankItemDecoration());
        rankAdapter.setEmptyView(emptyView);
        rvRank.setAdapter(rankAdapter);
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
        } else {
            request();
        }
    }

    private void request() {
        PracticeType practiceType = PracticeTypeUtils.getPracticeType();
        Map<String, String> map = new HashMap<>();
        map.put("carType", practiceType.licenseType.getTypeName());
        map.put("course", practiceType.subjectType.getTypeName() + "");
        subscribe(Api.getApi().rankList(RequestParameter.generate().getSingedParams(map)),
                request(data -> getData((RankResponse) data), true));
    }

    private void getData(RankResponse rankResponse) {
        rankAdapter.setNewData(rankResponse.getList());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void finishEvent(RankFinishEvent rankFinishEvent) {
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void rankEvent(RankEvent rankEvent) {
        token = mmkv.decodeString(MMKVConstant.TOKEN);
        request();
    }
}
